using System;
using System.Collections.Generic;
using System.Text;

namespace GetTogether.Studio.Schedule
{
    public enum ScheduleType
    {
        Service = 0,
    }

    public enum ScheduleStatus
    {
        Disabled = 0,
        Enabled = 1
    }

    public class ScheduleTask : GetTogether.ObjectBase.SerializationBase<ScheduleTask>
    {
        #region Attributes

        private string _ScheduleName;

        public string Name
        {
            get { return _ScheduleName; }
            set { _ScheduleName = value; }
        }

        private int _Week;

        public int Week
        {
            get { return _Week; }
            set { _Week = value; }
        }

        public DateTime ScheduleDateTime
        {
            get
            {
                DateTime _ScheduleDateTime = DateTime.MinValue;
                if (!string.IsNullOrEmpty(Date))
                {
                    _ScheduleDateTime = GetTogether.Utility.DateHelper.ConvertDate(Date, GetTogether.Utility.DateHelper.DateFormat.ddMMyyyys);
                    if (_ScheduleDateTime == DateTime.MinValue)
                    {
                        DateTime.TryParse(Date, out _ScheduleDateTime);
                    }
                }
                if (!string.IsNullOrEmpty(Time))
                {
                    TimeSpan ts = TimeSpan.Parse(Time);
                    _ScheduleDateTime = _ScheduleDateTime.AddHours(ts.Hours);
                    _ScheduleDateTime = _ScheduleDateTime.AddMinutes(ts.Minutes);
                    _ScheduleDateTime = _ScheduleDateTime.AddSeconds(ts.Seconds);
                    _ScheduleDateTime = _ScheduleDateTime.AddMilliseconds(ts.Milliseconds);
                }
                return _ScheduleDateTime;
            }

        }

        private string _Date;

        public string Date
        {
            get { return _Date; }
            set { _Date = value; }
        }

        private string _Time;

        public string Time
        {
            get { return _Time; }
            set { _Time = value; }
        }

        private bool _RepeatForTime;

        public bool RepeatForTime
        {
            get { return _RepeatForTime; }
            set { _RepeatForTime = value; }
        }

        private DateTime _ActionOn;

        public DateTime ActionOn
        {
            get { return _ActionOn; }
            set { _ActionOn = value; }
        }

        private ScheduleType _Type;

        public ScheduleType Type
        {
            get { return _Type; }
            set { _Type = value; }
        }

        private string _Url;

        public string Url
        {
            get { return _Url; }
            set { _Url = value; }
        }

        private ScheduleStatus _Status;

        public ScheduleStatus Status
        {
            get { return _Status; }
            set { _Status = value; }
        }

        private bool _IsInvoking;

        public bool IsInvoking
        {
            get { return _IsInvoking; }
            set { _IsInvoking = value; }
        }

        private bool _IsSendReport;

        public bool IsSendReport
        {
            get { return _IsSendReport; }
            set { _IsSendReport = value; }
        }

        private string _Category;

        public string Category
        {
            get
            {
                if (string.IsNullOrEmpty(_Category) || this._Category == "Unknown")
                {
                    if (!string.IsNullOrEmpty(Url) && this.Name != "GetTogether")
                    {
                        string[] addressInfo = Url.Split(new string[] { "Data Source" }, StringSplitOptions.RemoveEmptyEntries);
                        if (addressInfo.Length > 0)
                        {
                            return addressInfo[(addressInfo.Length >= 2 ? 1 : 0)].Split(';')[0].Replace("=", "");
                        }
                        else
                        {
                            return "Unknown";
                        }
                    }
                    else
                        return "Unknown";
                }
                return _Category;
            }
            set { _Category = value; }
        }


        #endregion

        public ScheduleTask()
        {
            _Week = 127;
            _ScheduleName = "Schedule Name";
            _Date = GetTogether.Utility.DateHelper.FormatDateTimeToString(DateTime.MinValue, GetTogether.Utility.DateHelper.DateFormat.ddMMyyyys);
            _Time = "18:00";
            _Type = ScheduleType.Service;
            _Status = ScheduleStatus.Enabled;
            _Url = "http://www.xxx.com";
        }

        #region Functions

        public static List<ScheduleTask> GetSchedules()
        {
            string path = GetTogether.Studio.ProjectParameter.GetSettingsPathPublic(GetTogether.Studio.ProjectParameter.ProjectCategory.Schedule);
            if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);
            List<ScheduleTask> pjs = new List<ScheduleTask>();
            foreach (string s in System.IO.Directory.GetFiles(path))
            {
                ScheduleTask pj = GetTogether.Utility.SerializationHelper.FromXml<ScheduleTask>(System.IO.File.ReadAllText(s, System.Text.Encoding.UTF8));
                pjs.Add(pj);
            }
            return pjs;
        }

        public static ScheduleTask GetScheduleByName(string name)
        {
            List<ScheduleTask> schs = GetSchedules();
            foreach (ScheduleTask s in schs)
            {
                if (s.Name == name) return s;
            }
            return null;
        }

        public static string GetSettingsPathPublic()
        {
            return GetTogether.Studio.ProjectParameter.GetSettingsPathPublic(GetTogether.Studio.ProjectParameter.ProjectCategory.Schedule);
        }

        public static ScheduleTask GetSettingsByProjectName(string projName)
        {
            return GetTogether.Studio.ProjectParameter.GetSettingsByProjectName<ScheduleTask>(GetTogether.Studio.ProjectParameter.ProjectCategory.Schedule, "", projName, "");
        }

        //public static string GetSettingsPath(string username)
        //{
        //    return GetTogether.Studio.ProjectParameter.GetSettingsPath(GetTogether.Studio.ProjectParameter.ProjectCategory.Schedule, username);
        //}

        //public static ProjectParameter GetSettingsByProjectName(string username, string projName, string shareUsername)
        //{
        //    return GetTogether.Studio.ProjectParameter.GetSettingsByProjectName<ProjectParameter>(GetTogether.Studio.ProjectParameter.ProjectCategory.Schedule, username, projName, shareUsername);
        //}

        #endregion
    }
}
